package org.lex.swing.widgets;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.MenuElement;

import org.lex.utils.IconCache;


public class JMenuButton extends JPanel implements ActionListener {
	private static final long serialVersionUID = 3485277362274010943L;
	private JButton mainButton = null;
	private JButton arrowButton = null;
	private JPopupMenu popup = null;
	private double weight = 0.85D;

	public JMenuButton() {
		super();
		initialize();
	}

	private void initialize() {
		this.setLayout(new BorderLayout());
		this.add(getMainButton(), java.awt.BorderLayout.CENTER);
		this.add(getArrowButton(), java.awt.BorderLayout.EAST);
	}

	private JButton getMainButton() {
		if (mainButton == null) {
			mainButton = new JButton();
		}
		return mainButton;
	}

	private JButton getArrowButton() {
		if (arrowButton == null) {
			arrowButton = new JButton();
			arrowButton.setIcon(IconCache.getIcon(IconCache.Arrow));
			arrowButton.setPreferredSize(new java.awt.Dimension(15, 13));
			arrowButton.addActionListener(this);
		}
		return arrowButton;
	}

	private JPopupMenu getPopup() {
		if (popup == null) {
			popup = new JPopupMenu();
		}
		return popup;
	}

	public void actionPerformed(ActionEvent e) {
		this.getPopup().show(this, 0, this.getHeight());
	}

	public void updateUI() {
		super.updateUI();
		this.getMainButton().updateUI();
		this.getArrowButton().updateUI();
		this.getPopup().updateUI();
		MenuElement[] menus = this.getPopup().getSubElements();
		for (MenuElement menu : menus)
			((JMenuItem) menu).updateUI();
	}

	/**
	 * �����ӿ�
	 */
	public void setFont(Font font) {
		this.getMainButton().setFont(font);
		MenuElement[] menus = this.getPopup().getSubElements();
		for (int i = 0; i < menus.length; i++)
			((JMenuItem) menus[i]).setFont(font);
	}

	public void setText(String text) {
		this.getMainButton().setText(text);
	}

	public void setSize(int width, int height) {
		int mainButtonWidth = (int) (width * this.weight);
		this.getMainButton().setSize(mainButtonWidth, height);
		this.getArrowButton().setSize(width - mainButtonWidth, height);
		super.setSize(width, height);
	}

	public void setWeight(double w) {
		this.weight = w;
		this.setSize(this.getWidth(), this.getHeight());
	}

	public void addSeparator() {
		this.getPopup().addSeparator();
	}

	public void add(JMenuItem menuItem) {
		this.getPopup().add(menuItem);
	}

	public void remove(JMenuItem menuItem) {
		this.getPopup().remove(menuItem);
	}

	public void setActionCommand(String ac) {
		this.mainButton.setActionCommand(ac);
	}

	public void addActionListener(ActionListener listener) {
		this.getMainButton().addActionListener(listener);
		MenuElement[] menus = this.getPopup().getSubElements();
		for (int i = 0; i < menus.length; i++)
			((JMenuItem) menus[i]).addActionListener(listener);
	}

	public void removeActionListener(ActionListener listener) {
		this.getMainButton().removeActionListener(listener);
		MenuElement[] menus = this.getPopup().getSubElements();
		for (int i = 0; i < menus.length; i++)
			((JMenuItem) menus[i]).removeActionListener(listener);
	}
}
